import { Request, Response } from "express";
import { GoodsService } from "../../services/goods.service";

export class Controller {
    // 获取所有类别接口
    static get(_req: Request, resp: Response): void {
        GoodsService.getAllCategory().then((data) => {
            resp.status(200).json({ msg: 'ok', datas: data });
        }).catch((error) => {
            resp.status(200).json({ msg: 'error', error });
        })
    }
    // 新增类别接口
    static add(req: Request, resp: Response): void {
        // 解析请求参数
        const name = req.body.name;
        const description = req.body.description;
        const sort = req.body.sort;
        GoodsService.addCategory({ name, description, sort }).then(() => {
            resp.status(200).json({ msg: 'ok' });
        }).catch((error) => {
            resp.status(200).json({ msg: 'error', error });
        });
    }
    // 更新类别接口
    static update(req: Request, resp: Response): void {
        const name = req.body.name;
        const id = req.body.id;
        const description = req.body.description;
        let sort = req.body.sort;

        if (!sort) {
            sort = 0;
        }
        if (!id) {
            resp.status(200).json({ msg: 'error', error: '请指定要更新的类别id' });
        }
        GoodsService.updateCategory({ name, id, description, sort }).then(() => {
            resp.status(200).json({ msg: 'ok' });
        });
    }
    // 新增商品
    static goodsAdd(req: Request, resp: Response): void {
        const name = req.body.name;
        const description = req.body.description;
        const detail = req.body.detail;
        const category = req.body.category;
        const price = req.body.price;
        const discounted_price = req.body.discounted_price;
        const stock = req.body.stock;
        const image = req.body.image;
        const status = req.body.status;
        const brand = req.body.brand;

        GoodsService.addGoods({ name, description, detail, category, price, discounted_price, stock, image, status, brand }).then(() => {
            resp.status(200).json({ msg: 'ok' });
        }).catch((error) => {
            resp.status(200).json({ msg: 'error', error });
        });
    }
    // 更新商品
    static goodsUpdate(req: Request, resp: Response): void {
        const id = req.body.id;
        const name = req.body.name;
        const description = req.body.description;
        const detail = req.body.detail;
        const category = req.body.category;
        const price = req.body.price;
        const discounted_price = req.body.discounted_price;
        const stock = req.body.stock;
        const image = req.body.image;
        const status = req.body.status;
        const brand = req.body.brand;

        GoodsService.updateGoods({ id, name, description, detail, category, price, discounted_price, stock, image, status, brand }).then(() => {
            resp.status(200).json({ msg: 'ok' });
        }).catch((error) => {
            resp.status(200).json({ msg: 'error', error });
        });
    }
    // 删除商品
    static goodsDelete(req: Request, resp: Response): void {
        const id = req.body.id;
        GoodsService.deleteGoods(id).then(() => {
            resp.status(200).json({ msg: 'ok' });
        }).catch((error) => {
            resp.status(200).json({ msg: 'error', error });
        });
    }
    // 获取商品详情接口
    static goodsGet(req: Request, resp: Response): void {
        const id = Number(req.query.id as string);
        GoodsService.getGoodsDetail(id).then((data) => {
            resp.status(200).json({ msg: 'ok', datas: data });
        }).catch((error) => {
            resp.status(200).json({ msg: 'error', error });
        });
    }
    // 查询商品列表
    static goodsListGet(req: Request, resp: Response): void {
        let cid = undefined;
        if (req.query.cid) {
            cid = Number(req.query.cid as string);
        }
        let offset = Number(req.query.offset as string);
        let limit = Number(req.query.limit as string);
        GoodsService.getGoodList(cid, offset, limit).then((data) => {
            resp.status(200).json({ msg: 'ok', datas: data });
        }).catch((error) => {
            resp.status(200).json({ msg: 'error', error });
        })
    }
}